package top.myhdg.bijou;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class UADialog extends Dialog {
    private CustomUa customUa;
    EditText editUA;
    private Button uaCustom;
    private String ua;//原始UA

    UADialog(@NonNull Context context, int themeResId, CustomUa customUa, String ua) {
        super(context, themeResId);
        this.customUa = customUa;
        this.ua = ua;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //==========设置更改UADialog位置及动画==========
        Window window = getWindow();
        assert window != null;
        window.setGravity(Gravity.CENTER); // 设置dialog显示位置
        window.setWindowAnimations(R.style.zoom_animation); // 添加动画效果
        setContentView(R.layout.dialog_ua);
        setCanceledOnTouchOutside(false);// 点击Dialog外部消失
        //==========设置更改UADialog位置及动画==========

        editUA = findViewById(R.id.editUA);
        editUA.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                editUA.setText(ua);
                editUA.selectAll();
            }
        });

        uaCustom = findViewById(R.id.buttonCustomUA);
        uaCustom.setEnabled(false);//未输入自定义UA时，应用按钮禁止点击
        editUA.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                uaCustom.setEnabled(true);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        Button exitChangeUA = findViewById(R.id.exitChangeUA);
        exitChangeUA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UADialog.this.dismiss();
            }
        });
    }

    //==========设置各按钮监听事件==========
    void setUaAndroidClickListener(View.OnClickListener onClickListener) {
        ImageButton uaAndroid = findViewById(R.id.uaAndroid);
        uaAndroid.setOnClickListener(onClickListener);
    }

    void setUaEdgeClickListener(View.OnClickListener onClickListener) {
        ImageButton uaEdge = findViewById(R.id.uaEdge);
        uaEdge.setOnClickListener(onClickListener);
    }

    void setUaChromeClickListener(View.OnClickListener onClickListener) {
        ImageButton uaChrome = findViewById(R.id.uaChrome);
        uaChrome.setOnClickListener(onClickListener);
    }

    void setUaIEClickListener(View.OnClickListener onClickListener) {
        ImageButton uaIE = findViewById(R.id.uaIE);
        uaIE.setOnClickListener(onClickListener);
    }

    void setUaIOSClickListener(View.OnClickListener onClickListener) {
        ImageButton uaIOS = findViewById(R.id.uaIOS);
        uaIOS.setOnClickListener(onClickListener);
    }

    void setUaCustomClickListener(View.OnClickListener onClickListener) {
        uaCustom.setOnClickListener(onClickListener);
    }
    //==========设置各按钮监听事件==========

    interface CustomUa {
        void getCustomUa(String ua);
    }

    @Override
    public void dismiss() {
        customUa.getCustomUa(editUA.getText().toString());//传回自定义UA
        super.dismiss();
    }
}
